// ==UserScript==
// @name         自动定位
// @namespace    http://www.rfbynet.club/
// @version      0.1
// @description  自动定位浏览位置
// @author       rorinl
// @match        https://*/*
// @exclude      https://googleads.g.doubleclick.net/*
// @exclude      https://www.baidu.com/*
// @exclude      https://cn.bing.com/*
// @exclude      https://home.firefoxchina.cn/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// @resource css http://liang-ronglin.gitee.io/domaindata/autopos.css
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_listValues
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @grant        GM_deleteValue
// ==/UserScript==

(function() {
    'use strict';
    GM_addStyle(GM_getResourceText("css"));
    if(self.frameElement && self.frameElement.tagName == "IFRAME") {
        console.log("in iframe");
    }else{
        var mmm = document.createElement("div");
        mmm.setAttribute("id", "autopost");
        mmm.innerHTML =`<div class="autopost_main">
                <ul class="autopost_nav">
                    <li>所有历史</li>
                </ul>
                <ul class="autopost_navcons">
                    <li class="active">
                        <ul>
                            <li><a>asadada</a></li>
                            <li><a>asadada</a></li>
                            <li><a>asadada</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
            <div id="showtips"></div>`;
        document.body.appendChild(mmm);
    }

    window.onload=function(){
        if(self.frameElement && self.frameElement.tagName == "IFRAME") {
            console.log("in iframe");
        }else{
        var ror_locathref = window.location.href;
        window.scrollTo(0,GM_getValue(ror_locathref,0));
        showtips((GM_getValue(ror_locathref,'')==''?1:0));
        var autopost = document.querySelector('#autopost');
        var navlist = document.querySelectorAll('#autopost .autopost_nav>li');
        autopost.onclick = function(e){ //红点开启关闭
            if(e.target.id == "autopost"){/**/
            var automain = document.querySelector('#autopost .autopost_main');
            var maintype = getStyle(automain, 'display');;
            automain.style.display = (maintype=="block"?"none":"block");
            autopost.style.cursor = (maintype=="block"?"pointer":"auto");
            }
        }
        navOne(document.querySelectorAll('#autopost .autopost_navcons>li')[0]); //初始化菜单-所有历史

        for(let i = 0;i < navlist.length; i++){
            navlist[i].onclick = (e)=>{ //菜单切换
                e.stopPropagation();
                var navcons = document.querySelectorAll('#autopost .autopost_navcons>li');
                for(var j = 0;j < navcons.length;j++){
                    navcons[j].className = "";
                }
                navcons[i].className = "active";
                switch(i){
                    case 0: //所有历史
                        navOne(navcons[i]);
                        break;
                }
            }
        }

        function showtips(tipsnode){
            var showtipss = ['哈喽😘,你上次阅读到这里哦!','嘿嘿,好好学习天天向上🤓!'];
            var showtime = 2500;
            var showtipsobj = document.querySelector("#showtips");
            showtipsobj.innerText = showtipss[tipsnode];
            showtipsobj.style.opacity = 1;
            setTimeout(()=>{
                showtipsobj.style.opacity = 0;
            },showtime)
        }

        function getStyle(element, attr) { //获取元素css属性
            if(element.currentStyle) {
                return element.currentStyle[attr];
            } else {
                return getComputedStyle(element, false)[attr];
            }
        }

        function navOne(domobj){ //菜单-所有历史
            var curGmList = GM_listValues();
            var curGmListleng = curGmList.length;
            var curconhtml = `<p><input type="button" value="删除" id="adelete" /><input type="button" value="全选" id="checkall" /> 共计<span id="linkslength">${curGmListleng}</span>条记录</p><ul>`;
            for(var aa = 0;aa<curGmListleng;aa++){
                curconhtml += (`<li><input type="checkbox" name="links[]" value="${aa}" /><a href="${curGmList[aa]}" target="_blank" >${curGmList[aa]}</a></li>`);
            }
            curconhtml += "</ul>";
            domobj.innerHTML = curconhtml;
            setTimeout(()=>{
                  document.querySelector("#adelete").onclick=function(){
                      var alllinks = document.querySelectorAll("#autopost input[name='links[]']");
                      for(var k = 0;k<alllinks.length;k++){
                          if(alllinks[k].checked){
                              console.log(alllinks[k].nextSibling.innerText);
                              GM_deleteValue(alllinks[k].nextSibling.innerText);
                              document.querySelector("#linkslength").innerHTML = GM_listValues().length;
                              alllinks[k].parentNode.remove();
                          }
                      }
                  }
                  var iscall = true;
                  document.querySelector("#checkall").onclick=function(){
                      var alllinks = document.querySelectorAll("#autopost input[name='links[]']");
                      for(var k = 0;k<alllinks.length;k++){
                          alllinks[k].checked = iscall;
                      }
                      iscall = !iscall;
                  }
            },500);
        }
        }
    }


    window.onscroll = function() {
        if(self.frameElement && self.frameElement.tagName == "IFRAME") {
            console.log("in iframe");
        }else{
            var ror_scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var ror_locathref = window.location.href;
            GM_setValue(ror_locathref,ror_scrollTop);
        //console.log("滚动距离" + ror_scrollTop);
        }
    }
})();